class Solution {
public:
    int ret = 0;
    void mergeSort(vector<int>& s,int l,int r){
        if(l >= r) return;
        int mid = (l + r) / 2;
        mergeSort(s,l,mid);
        mergeSort(s,mid+1,r);
        
        int k = mid;
        int m = r;
        while(k >= l){
            while(m > mid && (long long)s[k] <= (long long)s[m] * 2) m--;
            ret += m - mid;
            k--;
        }
        
        vector<int> temp(r - l + 1);
        int i = l,j = mid+1;
        int index = 0;
        while(i <= mid || j <= r){
            if(i > mid){
                temp[index++] = s[j++];
            }else if(j > r){
                temp[index++] = s[i++];
            }else{
                if(s[i] <= s[j]){
                    temp[index++] = s[i++];
                }else{
                    temp[index++] = s[j++];
                }
            }
        }
        for(int n = 0;n < temp.size();n++){
            s[n + l] = temp[n];  
        }

    }
    int reversePairs(vector<int>& nums) {
        mergeSort(nums,0,nums.size()-1);
        return ret;
    }
};
